package org.zwt.request.impl;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.zwt.common.entity.HttpRequest;
import org.zwt.common.formatter.IRequestFormatter;
import org.zwt.request.IRequestHandler;

import java.io.IOException;
import java.io.InputStream;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SimpleRequestHandler implements IRequestHandler {
    private IRequestFormatter requestFormatter;


    @Override
    public HttpRequest handle(InputStream is) throws IOException {
        /*
         * 1.从is中读取内容,并转换成字符串
         * */
        int len = is.available();
        byte[] buffer = new byte[len];
        is.read(buffer);
        String data = new String(buffer, "UTF-8");
        /*
         * 2.调用IRequestFormatter的parse方法,获取HttpRequest对象
         * */
        return this.requestFormatter.parse(data);
    }
}
